<template>
  <div class="container">
    <div class="app-container">
      <div style="padding: 10px">
        <el-button type="primary" size="mini" @click="addPermissionClick('add')">添加权限</el-button>
      </div>
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermissionClick">添加</el-button>
            <el-button type="text" size="mini" @click="editPermissionClick(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delConfirmClick(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加和编辑权限的弹层 -->
    <el-dialog :title="permissionForm.id ? '编辑权限' : '添加权限'" width="500px" :visible.sync="showDialog" @close="close">
      <!-- 权限表单 -->
      <el-form ref="permissionForm" :model="permissionForm" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" size="mini" style="width:300px" type="textarea" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="permissionForm.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="submit">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/homework/position'

// import { transListToTree } from '@/utils'
export default {
  data() {
    return {
      // 权限列表数据
      permissionList: [],
      showDialog: false,
      permissionForm: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '1', // 权限开启状态："1" 开启 "0" 不开启，默认开启
        type: 1, // 权限类型：1 一级权限 2 二级权限
        pid: 0 // 父级权限ID
      },

      // 数据校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '标识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取权限列表
    this.getPermissions()
  },
  methods: {
    close() {
      this.showDialog = false
      this.$refs.permissionForm.resetFields()
      this.permissionForm = {}
    },
    // 获取权限列表
    async getPermissions() {
      // 调用权限列表接口
      const res = await getPermissionList()
      // console.log(res)
      this.permissionList = res.data
    },
    // 添加数据
    async addPermissionClick() {
      //  展示弹层
      this.showDialog = true
    },
    // 编辑
    async editPermissionClick(id) {
      //  展示弹层
      this.showDialog = true
      const res = await getPermissionDetail(id)
      // 数据回显成功
      this.permissionForm = res.data
    },
    // 表单确认
    submit() {
      this.$refs.permissionForm.validate(async valid => {
        if (!valid) return false
        if (this.permissionForm.id) {
          await updatePermission(this.permissionForm)
        } else {
          await addPermission(this.permissionForm)
        }
        // 提示
        this.$message.success(this.permissionForm.id ? '编辑权限' : '添加权限')
        this.getPermissions()
        this.close()
      })
    },
    // 删除delConfirmClick权限
    async delConfirmClick(id) {
      await delPermission(id)
      // 提示
      this.$message.success('删除权限成功')
      this.getPermissions()
    }
  }
}
</script>

<style>

</style>
